package xin.marcher.module.message.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import xin.marcher.module.common.constants.CacheConstant;
import xin.marcher.module.common.redis.RedisLock;
import xin.marcher.module.message.service.MessageService;

import javax.annotation.Resource;

/**
 * 负责定时验证是否需要兜底数据 DB 落地, 数据来源于 Cache 组件 DB 落库
 */
@Component
public class DataRefreshTask {

    @Resource
    private RedisLock redisLock;

    @Resource
    private MessageService messageService;

    /**
     * 每分钟验证下是否触发缓存DB兜底
     */
    @Scheduled(fixedDelay = 60000)
    public void start() {
        boolean lock = redisLock.lock(CacheConstant.CACHE_LOCK_KEY);
        try {
            if (lock) {
                messageService.outDataCacheRefresh();
            }
        } finally {
            redisLock.unlock(CacheConstant.CACHE_LOCK_KEY);
        }
    }
}
